// pages/register/register.js
import { getMerchantConfigs, smsSend, merchantRegister, getUserState } from '../../utils/networkRequest.js'
import { errorMsg } from '../../utils/tool.js'
import Net from '../../utils/network.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataInfo: null,
    downtime: 60,//验证码倒计时 

    phoneNumber: "",//手机号
    code: "",//验证码
    shopName: "",//店铺名称
    shopBossName: "",//店主姓名
    telPhone: "",//固定电话
    address: "",//详细地址
    ID: "",//验证id

    yyzzSrc: "",//营业执照注册时要提交到服务器的地址
    showImg: "",//营业执照再次进入选择图片页面时显示上次的地址

    jyfwStr: "",//经营范围字符串eg: 1,2,3,4 
    jyfwShowStr:"",//经营范围显示内容
    workersIndex: "",//店铺人数
    scalesIndex: "",//店铺规模

    islock: false,//用来防止点击发生两次跳转
    isShow: false,//控制页面是否显示
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;

    
    let value = wx.getStorageSync('token') //从本地缓存中获取Session信息 
    if (value) { 
      getUserState((r) => {
        if (r.code == 0) {
          if (r.data.identity == -1) {
            //普通用户
            self.setData({
              isShow: true
            })
            self.registerInit(); //注册信息初始化

          } else {
            //非普通用户
            wx.redirectTo({
              url: '../../pages/index/index',
            })
          }

        } else {
          errorMsg(r.message);
        }
      }) 
    } else { 
      setTimeout(() => { 
        getUserState((r) => {
          if (r.code == 0) {
            if (r.data.identity == -1) {
              //普通用户
              self.setData({
                isShow: true
              })
              self.registerInit(); //注册信息初始化

            } else {
              //非普通用户
              wx.redirectTo({
                url: '../../pages/index/index',
              })
            }
          } else {
            errorMsg(r.message);
          }
        }) 
      }, 1000)

    }
  },
  registerInit: function () {
    let self = this;
    getMerchantConfigs((r) => {
      if (r.code == 0) {
        debugger;
        self.setData({
          dataInfo: r.data
        })
        console.log(r.data)
      } else {
        errorMsg(e.message)
      }
    })

    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        app.globalData.qqmapsdk && app.globalData.qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (res) {
            self.setData({
              address: res.result.address
            })
          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        });
      }
    })
  },

  //跳转到营业范围选择并带上 选项列表
  goBusiness: function (e) {
    let self = this;
    if (!self.data.islock) {

      self.setData({
        islock: true
      })
      wx.navigateTo({
        url: '/pages/jyfwselect/jyfwselect?jyfwStr=' + self.data.jyfwStr + '&business=' + JSON.stringify(self.data.dataInfo.business),
        complete: function () {
          self.setData({
            islock: false
          })
        }
      })
    }


  },
  //发送验证码
  onSendCode: function (e) {
    let self = this;
    let { phoneNumber } = this.data;
    if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phoneNumber)) {
      errorMsg('手机号不正确');
    } else {
      if (this.data.downtime == 60) {
        smsSend(phoneNumber, (r) => {

          if (r.code == 0) {
            this.setData({
              downtime: --self.data.downtime
            })
            let siv = setInterval(() => {
              if (self.data.downtime > 1) {
                self.setData({
                  downtime: --self.data.downtime
                })
              } else {
                clearInterval(siv);
                self.setData({
                  downtime: 60
                })
              }

            }, 1000)
          } else {
            errorMsg(r.message);
          }
        })
      }
    }


  },
  //获取手机号
  phoneNumberChange: function (e) {
    this.setData({
      phoneNumber: e.detail.detail.value
    })
  },
  //获取验证码
  codeChange: function (e) {

    this.setData({
      code: e.detail.value
    })
  },
  //获取店铺名称
  shopNameChange: function (e) {
    this.setData({
      shopName: e.detail.detail.value
    })
  },
  //获取店主姓名
  shopBossNameChange: function (e) {
    this.setData({
      shopBossName: e.detail.detail.value
    })
  },
  //获取固定电话
  telPhoneChange: function (e) {
    this.setData({
      telPhone: e.detail.detail.value
    })
  },
  //获取详细地址
  addressChange: function (e) {
    this.setData({
      address: e.detail.detail.value
    })
  },
  //获取验证id
  idChange: function (e) {
    this.setData({
      ID: e.detail.detail.value
    })
  },
  //获取人数
  bindWorkersChange: function (e) {
    this.setData({
      workersIndex: e.detail.value
    })
  },
  //获取规模
  bindScalesChange: function (e) {
    this.setData({
      scalesIndex: e.detail.value
    })
  },
  //注册
  onSubmit: function () {

    let { phoneNumber, code, shopName, shopBossName, telPhone, address, ID, yyzzSrc, jyfwStr, workersIndex, scalesIndex } = this.data;
    if (!phoneNumber) {
      errorMsg("手机号不能为空");
    } else if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phoneNumber)) {
      errorMsg("手机号不正确");
    } else if (!code) {
      errorMsg("请输入验证码");
    } else if (!shopName) {
      errorMsg("请输入店铺名称");
    } else if (!shopBossName) {
      errorMsg("请输入店主姓名");
    } else if (!telPhone) {
      errorMsg("请输入固定电话");
    } else if (!address) {
      errorMsg("请输入店铺地址");
    } else if (!ID) {
      errorMsg("请输入邀请码");
    } else if (!yyzzSrc) {
      errorMsg("请上传营业执照");
    } else if (!jyfwStr) {
      errorMsg("请选择经营范围");
    } else {
      //name, mobile, verifyCode, address, telephone, master_name, invitecode, business, workers, scale,
      let business = "[" + jyfwStr + "]";
      let workersId = this.data.dataInfo.workers[workersIndex].id;
      let scaleId = this.data.dataInfo.scales[scalesIndex].id;
      merchantRegister(shopName, phoneNumber, code, address, telPhone, shopBossName, ID, business, workersId, scaleId, yyzzSrc, (r) => {
        if (r.code == 0) {
          wx.redirectTo({
            url: '/pages/index/index',
          })
        } else {
          errorMsg(r.message);
        }
      })
    }


  }

})